{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Theano 简介及其安装"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 简介"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`Theano` 是一个 `Python` 科学计算库，允许我们进行符号运算，并在 `CPU` 和 `GPU` 上执行。\n",
    "\n",
    "它最初由 `Montreal` 大学的机器学习研究者们所开发，用来进行机器学习的计算。\n",
    "\n",
    "按照[官网](http://deeplearning.net/software/theano/)上的说明，它拥有以下几个方面的特点：\n",
    "\n",
    "- 与 **Numpy, Scipy** 的紧密结合\n",
    "- **GPU** 加速\n",
    "- 高效的符号计算\n",
    "- 速度和稳定性\n",
    "- 动态生成 **C** 代码"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用 anaconda 安装 theano"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`windows` 下，使用 `anaconda` 安装 `theano` 的命令为：\n",
    "\n",
    "    conda install mingw libpython\n",
    "    pip install theano\n",
    "    \n",
    "`linux` 下，使用 `anaconda` 安装的命令为\n",
    "    \n",
    "    conda install theano\n",
    "\n",
    "安装好之后，还需要安装 `Cuda` 并进行 `GPU` 环境的配置，否则是不能利用 `GPU` 进行计算的，推荐使用 `linux/mac` 进行配置，具体方法可以参考[官网](http://deeplearning.net/software/theano/)上的配置说明。\n",
    "\n",
    "查看安装的版本："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0.7.0.dev-54186290a97186b9c6b76317e007844529a352f4'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import theano\n",
    "\n",
    "theano.__version__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "查看当前使用的 device："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'cpu'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "theano.config.device"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运行测试："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python2.7/dist-packages/theano/misc/pycuda_init.py:34: UserWarning: PyCUDA import failed in theano.misc.pycuda_init\n",
      "  warnings.warn(\"PyCUDA import failed in theano.misc.pycuda_init\")\n",
      "....................S..............."
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Theano version 0.7.0.dev-54186290a97186b9c6b76317e007844529a352f4\n",
      "theano is installed in /usr/local/lib/python2.7/dist-packages/theano\n",
      "NumPy version 1.10.1\n",
      "NumPy relaxed strides checking option: True\n",
      "NumPy is installed in /usr/lib/python2.7/dist-packages/numpy\n",
      "Python version 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2]\n",
      "nose version 1.3.7\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "----------------------------------------------------------------------\n",
      "Ran 37 tests in 37.919s\n",
      "\n",
      "OK (SKIP=1)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<nose.result.TextTestResult run=37 errors=0 failures=0>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "theano.test()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里我已经在本地 `Windows` 配好了 `GPU` 的设置，如果没有配好，显示的结果可能不一样。\n",
    "\n",
    "`Windows` 下第一次运行可能会显示 `DEBUG: nvcc STDOUT` 等内容，**`Just ignore it!`**"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
